package com.flash.DayProcessing.mapper;

import com.flash.DayProcessing.entity.BondsInterest;
import com.flash.DayProcessing.entity.CashInterest;
import com.flash.DayProcessing.entity.TowFees;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Mapper
@Repository
public interface AccrualOfIncomeMapper {
    //现金搜索利息
    //余额*年利率/数量/计息期间
    @Select("select round(money*(cardRetes/100/(case when bearingTime=1 then 360 when bearingTime=2 then 365 else 366 end)),2) as interest,\n" +
            "c.bankName,c.accountName,ca.money,c.bearingTime,c.cashId,\n" +
            "(case when c.stockpileType = 1 then 1 else 2 end) as stockpileType,\n" +
            "c.cardRetes,ca.dateCashStock,c.stockpileType\n" +
            "from  \n" +
            "(select c.*, rownum rn from (select * from cashStock where dateCashStock=#{dateCashStock}) c) ca \n" +
            "join cash c on ca.cashId=c.cashId\n" +
            "where 1=1 and RN BETWEEN #{page} and #{limit}")
    public List<CashInterest> selectCashInterest(HashMap hashMap);

    @Select("select count(*) from (select * from cashStock) c join (select * from cash) ca on c.cashId=ca.cashId")
    public int selectCashInterestCount();

    //债券数量*票面金额*票面利率/100/365
    @Select("select b.bondName,b.bondId,b.delayDate,ss.quantity,b.interestTime,b.faceAmount,ss.dateStock,b.bearingStart,ss.dateStock,ss.securityId,\n" +
            "round((ss.quantity*b.faceAmount*b.delayDate/100/365),2) as interest\n" +
            "from \n" +
            "(select s.*, rownum rn from (select * from securityStock where dateStock=#{dateStock}) s) ss\n" +
            "join \n" +
            "(select * from bond) b\n" +
            "on ss.fundId=b.bondId\n" +
            "where 1=1 and RN BETWEEN #{page} and #{limit}")
    public List<BondsInterest> selectBondsInterest(HashMap hashMap);

    @Select("select count(*) from \n" +
            "(select * from securityStock)ss\n" +
            "join\n" +
            "(select * from bond) b\n" +
            "on ss.fundId=b.bondId")
    public int selectBondsInterestCount();

    //资产净值*管理人费率/100/365
    //资产净值*托管人费率/100/365
    @Select("select f.fundId,f.trusteeCostRate,f.custodianCostRate,f.cashId,ts.statisticsDate,ts.notValueCost,c.dateCashStock,\n" +
            "round((ts.notValueCost*f.trusteeCostRate/100/365),4)as trusteeInterest,\n" +
            "round((ts.notValueCost*f.custodianCostRate/100/365),4)as custodianInterest\n" +
            " from \n" +
            " (select fd.*,rownum rn from (select * from fund ) fd)f\n" +
            "join\n" +
            "(select * from cashStock where dateCashStock=#{dateCashStock}) c\n" +
            "on f.fundId=c.fundId\n" +
            "join\n" +
            "(select * from netValueStatistics where projectName='资产净值' and statisticsDate= to_char(to_date('2022-08-25','yyyy-MM-dd')-1,'yyyy-MM-dd')) ts\n" +
            "on c.fundId=ts.fundId\n" +
            "and rn BETWEEN #{page} and #{limit}")
    public List<TowFees> selectTowFeesInterest(HashMap hashMap);

    @Select("select count(*) from \n" +
            "(select * from fund) f\n" +
            "join\n" +
            "(select * from netValueStatistics where projectName='资产净值') ts\n" +
            "on f.fundId=ts.fundId")
    public int selectTowFeesInterestCount();
}
